import request from '@/router/axios';
import Mock from 'mockjs';
import { Random } from 'mockjs'
//手机号
// 创建自定义 Mock 函数
Random.extend({
    // 自定义函数名: function 函数  
    telFn: function() {    
      const phonePrefix = ['132', '135', '189','156','138','181','186']
      const index = Math.floor(Math.random() * phonePrefix.length)
      var phone = phonePrefix[index] + Mock.mock(/\d{8}/);
      return this.pick(phone)
  }
})

// var ids = Mock.mock({"id|1-300":1000})

let list = Mock.mock({
    "tableList|160-300":[
        {
            'id|1-899':3000,
            username:'@cname',
            "gender|0-1":100, // 0 女  1 男
            email:'@email',
            phone:'@telFn()',
            "userLevel|1-2":10, // 1管理员  2普通用户
            "status|0-1":10,   // 0 可用  1 不可用
            birth:'@date("yyyy-MM-dd")'  
        }
    ]
})

// 获取用户数据列表
export const UserList = (data) =>request({
    url:'/UserList',
    method:'get',
    data
})
Mock.mock(/\/UserList/,config=>{
    let {pagesize,pagenum} = JSON.parse(config.body);
    let _list = list.tableList; //整体数据
    //总条数
    let total = _list.length;
    //返回当前页数的对应的pagesize数量的数据
    if(pagesize && pagenum){
        let user = _list.filter((item,index)=>index<pagesize*pagenum && index>=pagesize*(pagenum-1));
        return {
            data:{
                user,total,pagenum,pagesize
            },
            meta:{
                msg:'请求成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'获取失败',
                status:400
            }
        }
    }
})
//添加用户
export const addUser = (data) =>request({
    url:'/addUser',
    mothed:'post',
    data
});
Mock.mock(/\/addUser/,config=>{
    var ids = Mock.mock({"id|900-999":1000})
    let datas = JSON.parse(config.body)
    let {username,gender} = datas;
    let _list = list.tableList;
    if(username && gender){
        _list.unshift({
            id:ids.id,
            username,
            gender,
            email:datas.email?datas.email:'',
            phone:datas.phone?datas.phone:'',
            userLevel: 1,
            status: 0,
            birth:datas.birth?datas.birth:'',  
        })
        return {
            meta:{
                msg:'添加成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'添加失败',
                status:400
            }
        }
    }
    
})

//删除数据
export const detUser = (data) =>request({
    url:'/detUser',
    mothed:'delete',
    data
})
Mock.mock(/\/detUser/,config=>{
    let {id} = JSON.parse(config.body);
    let _list = list.tableList;
    if(id){
        _list.splice((_list.findIndex(item=>item.id==id)),1)
        return {
            meta:{
                msg:'删除成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'删除失败',
                status:400
            }
        }
    }
    
})

//查询数据 根据手机号 和用户名
export const queryUser = (data) =>request({
    url:'/queryUser',
    mothed:'get',
    data
})
Mock.mock(/\/queryUser/,config=>{
    let datas = JSON.parse(config.body);
    let _list = list.tableList;
    let arr = [];
    if(datas.username || datas.phone){
        if(datas.username){
            arr = _list.filter((item,index)=>item.username.indexOf(datas.username)!=-1);
        }
        if(datas.phone){
            arr = (arr.length==0?_list:arr).filter((item,index)=>item.phone.indexOf(datas.phone)!=-1);
        }
        let total = arr.length;
        let pagesize = arr.length
        return {
            data:{
                arr,total,pagesize
            },
            meta:{
                msg:'获取成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
})

// 修改数据
export const editUser = (data) =>request({
    url:'/editUser',
    mothed:'put',
    data
})
Mock.mock(/\/editUser/,config=>{
    let datas = JSON.parse(config.body);
    let {id} = datas;
    let _list = list.tableList;
    if(id){
        if(datas.email || datas.phone){
            for(let item of _list){
                if(item.id==id){
                    item.email = datas.email?datas.email:item.email;
                    item.phone = datas.phone?datas.phone:item.phone
                }
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
   
})

// 修改用户状态
export const alterUser = (data) =>request({
    url:'/alterUser',
    mothed:'put',
    data
})
Mock.mock(/\/alterUser/,config=>{
    let datas = JSON.parse(config.body);
    let {id,status} = datas;
    let _list = list.tableList;
    if((status==0 || status==1) && id){
        for(let item of _list){
            if(item.id==id){
                item.status = status;
            }
        }
        return {
            meta:{
                msg:'修改成功',
                status:200
            }
        }
    }else{
        return {
            meta:{
                msg:'参数错误',
                status:400
            }
        }
    }
    
})